/*
 * libc/crypto/base64.c
 */

#include <base64.h>

static const char encode_table[] = {
	'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
	'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
	'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
	'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
	'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
	'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
	'w', 'x', 'y', 'z', '0', '1', '2', '3',
	'4', '5', '6', '7', '8', '9', '+', '/',
};

static const char decode_table[] = {
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
	0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
	0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
	0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
	0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
	0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
	0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
	0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
	0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
};

int base64_encode(const char * in, int len, char * out)
{
	int i, j, s;
	char l, c;

	for(i = j = 0, s = 0, l = 0; i < len; i++)
	{
		c = in[i];
		switch(s)
		{
		case 0:
			s = 1;
			out[j++] = encode_table[(c >> 2) & 0x3f];
			break;
		case 1:
			s = 2;
			out[j++] = encode_table[((l & 0x3) << 4) | ((c >> 4) & 0xf)];
			break;
		case 2:
			s = 0;
			out[j++] = encode_table[((l & 0xf) << 2) | ((c >> 6) & 0x3)];
			out[j++] = encode_table[c & 0x3f];
			break;
		default:
			break;
		}
		l = c;
	}
	switch(s)
	{
	case 1:
		out[j++] = encode_table[(l & 0x3) << 4];
		out[j++] = '=';
		out[j++] = '=';
		break;
	case 2:
		out[j++] = encode_table[(l & 0xf) << 2];
		out[j++] = '=';
		break;
	default:
		break;
	}
	out[j] = '\0';
	return j;
}

int base64_decode(const char * in, int len, char * out)
{
	int i, j;
	char c;

	for(i = j = 0; i < len; i++)
	{
		if(in[i] == '=')
			break;
		if((in[i] < '+') || (in[i] > 'z'))
			return 0;
		c = decode_table[(unsigned char)in[i]];
		if(c == 0xff)
			return 0;
		switch(i & 0x3)
		{
		case 0:
			out[j] = (c << 2) & 0xff;
			break;
		case 1:
			out[j++] |= (c >> 4) & 0x3;
			out[j] = (c & 0xF) << 4;
			break;
		case 2:
			out[j++] |= (c >> 2) & 0xf;
			out[j] = (c & 0x3) << 6;
			break;
		case 3:
			out[j++] |= c;
			break;
		default:
			break;
		}
	}
	return j;
}
